package frame.options.physicalNetworks;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import model.components.networks.network.PhysicalNetwork;
import frame.MopraFrame;
import frame.networks.tables.NetworksTableTabbedPane;
import frame.networks.tables.PhysicalNetworksTableModel;
import frame.networks.tables.PhysicalNetworksTable;
import frame.networks.viewers.PhysicalNetworksVisualizationViewersTabbedPane;
import frame.options.physicalNetworks.create.CreatePhysicalNetworksWizard;

@SuppressWarnings("serial")
public class CreatePhysicalNetworksMenuItem extends JMenuItem implements ActionListener
{
	private MopraFrame mopraFrame;
	public MopraFrame getFrame(){ return mopraFrame; }
	public void setFrame( MopraFrame mopraFrame ){ this.mopraFrame = mopraFrame; }

	public CreatePhysicalNetworksMenuItem( MopraFrame mopraFrame )
	{
		this.setFrame( mopraFrame );
		
		this.setText( "Create" );
		this.setMnemonic( KeyEvent.VK_C );
		this.setIcon( new ImageIcon( this.getClass().getResource( "/icons/create.png" ) ) );
		this.addActionListener( this );
	}
	
	@Override
	public void actionPerformed( ActionEvent actionEvent )
	{
		NetworksTableTabbedPane networksTableTabbedPane = this.getFrame().getNetworksPanel().getNetworksTablesTabbedPane();
		CreatePhysicalNetworksWizard createPhysicalNetworksWizard = new CreatePhysicalNetworksWizard( this.getFrame() );
		List< PhysicalNetwork > simulatorPhysicalNetworks = this.getFrame().getSimulator().getPhysicalNetworks();
		PhysicalNetworksVisualizationViewersTabbedPane physicalNetworksVisualizationViewersTabbedPane = this.getFrame().getNetworksPanel().getPhysicalNetworksVisualizationViewersTabbedPane();
		PhysicalNetworksTable physicalNetworksTable = networksTableTabbedPane.getPhysicalNetworksTable();
		PhysicalNetworksTableModel physicalNetworksTableModel = (PhysicalNetworksTableModel) physicalNetworksTable.getModel();
		SavePhysicalNetworksMenuItem savePhysicalNetworksMenuItem = this.getFrame().getOptions().getPhysicalNetworksMenu().getSavePhysicalNetworksMenuItem();
		
		networksTableTabbedPane.setSelectedIndex( 0 );
		
		if( createPhysicalNetworksWizard.showModalDialog() == 0 )
		{	
			simulatorPhysicalNetworks.clear();
			physicalNetworksVisualizationViewersTabbedPane.removeAll();
			physicalNetworksTable.removeAll();
			
			for( PhysicalNetwork physicalNetwork: createPhysicalNetworksWizard.getPhysicalNetworks() )
			{
				simulatorPhysicalNetworks.add( physicalNetwork );
				physicalNetworksVisualizationViewersTabbedPane.addPhysicalNetworkTab( physicalNetwork );
				physicalNetworksTableModel.addRow( new Object[]{ physicalNetwork.getName(), physicalNetwork.getState() } );
			}
			
			physicalNetworksTableModel.refresh();
			
			if( simulatorPhysicalNetworks.size() == 0 )
				savePhysicalNetworksMenuItem.setEnabled( false );
			else
				savePhysicalNetworksMenuItem.setEnabled( true );
		}
	}
	
}
